package com.xaicode.auth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xaicode.auth.base.BaseMpServiceImpl;
import com.xaicode.auth.constant.EnableEnum;
import com.xaicode.auth.entity.SysPost;
import com.xaicode.auth.mapper.SysPostMapper;
import com.xaicode.auth.security.domain.params.SysPostQueryParam;
import com.xaicode.auth.service.ISysPostService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysPostServiceImpl extends BaseMpServiceImpl<SysPostMapper, SysPost> implements ISysPostService {


    @Override
    public PageInfo<SysPost> queryPage(SysPostQueryParam query) {
        startPage(query);
        List<SysPost> list = queryList(query);
        return PageInfo.of(list);
    }

    @Override
    public List<SysPost> queryList(SysPostQueryParam query) {
        LambdaQueryWrapper<SysPost> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(query.getPostName()), SysPost::getPostName, query.getPostName());
        List<SysPost> list = list(lqw);
        return list;
    }

    @Override
    public List<Integer> getPostIdsByUserId(String userId) {
        List<SysPost> posts = baseMapper.getPostsByUserId(userId, EnableEnum.ENABLE.getCode());
        return posts.stream()
                .map(SysPost::getId)
                .distinct()
                .collect(Collectors.toList());
    }

}
